package biznussoft.metercor.fragments;

import com.salesforce.androidsdk.app.SalesforceSDKManager;

import biznussoft.metercor.R;
import biznussoft.metercor.listeners.InventoryListener;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class StockTransfer extends Fragment implements OnItemClickListener{
	ListView list;
	InventoryListener listener;
	
	String strArray[]={"Aramark Philadelphia Inventory Site","Aramark West HeadQuarters","Aramark East HeadQuarters","Aramark main Warehouse"};
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.stock_transfer, null);
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
		MenuItem actionItem = menu.add("Logout");                 
		MenuItemCompat.setShowAsAction(actionItem,MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		actionItem.setIcon(R.drawable.logout); 
		MenuItem mytrack = menu.add("MyTruck");
		MenuItemCompat.setShowAsAction(mytrack, MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_WITH_TEXT);
		mytrack.setIcon(R.drawable.truck); 

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if(item.getTitle().equals("Logout")){
			SalesforceSDKManager.getInstance().logout(getActivity());}
		 if(item.getTitle().equals("MyTruck")){
			 listener.onWarehouseClicked(1, "Truck#4724");
			 
		 }
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		
		listener = (InventoryListener) getActivity();
	}

	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		
		list = (ListView) getView().findViewById(R.id.listview);
		
		/*ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,strArray);
		list.setAdapter(adapter);*/
		
		Display adapter = new Display(strArray);
		list.setAdapter(adapter);
		list.setOnItemClickListener(this);
		
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		listener.onWarehouseClicked(0, "Aramark Philadelphia Inventory Site");
		
	}
	
	
	class Display extends BaseAdapter {

		String [] ary;
		public Display(String[] strArray) {
			// TODO Auto-generated constructor stub
			ary = strArray;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return strArray.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View v, ViewGroup arg2) {
			// TODO Auto-generated method stub
			
			LayoutInflater inflator = getActivity().getLayoutInflater();
			v = inflator.inflate(R.layout.details, null);
			
			TextView text = (TextView) v.findViewById(R.id.text);
			text.setText(ary[position]);
			return v;
		}
		
	}
	
}
